package sg.edu.nus.iss.jass.gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import sg.edu.nus.iss.jass.controller.MainMenuController;

/**
 * @version 1.0
 * @author SE19 Team 1
 * 
 */
public class ToolbarMenu extends JToolBar {
	private static final long serialVersionUID = 1L;
	
	public static final String BTN_ADD_JOBADVERT = "btnAddJobAdvert";
	public static final String BTN_SEARCH_JOBADVERT = "btnSearchJobAdvert";
	public static final String BTN_RESPONSE = "btnResponse";
	public static final String BTN_LOGOUT = "btnLogout";

	private Properties properties;
	private MainMenuController mainMenuController;
	private AbstractButton btnAddJobAdvert, btnSearchJobAdvert, btnResponse, btnLogout;
	
	public ToolbarMenu (MainMenuController mainMenuController) {
		loadConfiguration();
		this.mainMenuController = mainMenuController;
		
		List<AbstractButton> subItems = new ArrayList<AbstractButton>();
        
		// 1. Menu for Job Advert.
        subItems = new ArrayList<AbstractButton>();
		subItems.add(btnAddJobAdvert = new JMenuItem(properties.getProperty("mainMenu.label.addJobAdvert")));
		btnAddJobAdvert.setName(BTN_ADD_JOBADVERT);
        subItems.add(btnSearchJobAdvert = new JMenuItem(properties.getProperty("mainMenu.label.searchJobAdvert")));
        btnSearchJobAdvert.setName(BTN_SEARCH_JOBADVERT);
        this.add(GetToolbarButton(properties.getProperty("mainMenu.label.jobAdvert"), subItems));
        
        // 2. Menu for Response.
        btnResponse = GetToolbarButton(properties.getProperty("mainMenu.label.response"));
        btnResponse.setName(BTN_RESPONSE);
        this.add(btnResponse);
        
        // 3. Menu for Logout.
        btnLogout = GetToolbarButton(properties.getProperty("mainMenu.label.logout"));
        btnLogout.setName(BTN_LOGOUT);
        this.add(btnLogout);
        
        this.setLayout(new GridLayout(1,5));
        this.setFloatable(false);
        this.setPreferredSize(new Dimension(StyleConstants.WINDOW_WIDTH, 50));
        
	}
	
	private AbstractButton GetToolbarButton(String name, List<AbstractButton> subItems) {
        final JToggleButton button = new JToggleButton(name);
        final List<AbstractButton> items = new ArrayList<AbstractButton>();
        
        for(AbstractButton menuItem : subItems) {
            items.add(menuItem);
            menuItem.setFont(new Font("Calibri", Font.BOLD, 18));
            menuItem.addActionListener(mainMenuController);
        }
        
        button.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    createAndShowMenu((JComponent) e.getSource(), button, items);
                }
            }
        });
        
        button.setFocusable(false);
        button.setFont(new Font("Calibri", Font.BOLD, 18));
        return button;
    }
	
	private AbstractButton GetToolbarButton(String name) {
        final JButton button = new JButton(name);
        
        button.setFont(new Font("Calibri", Font.BOLD, 18));
        button.setHorizontalTextPosition(SwingConstants.LEADING);
        button.setFocusable(false);
        button.addActionListener(mainMenuController);
        return button;
    }
	
	private void createAndShowMenu(JComponent component, final JToggleButton button, List<AbstractButton> items) {
        JPopupMenu menu = new JPopupMenu();
        menu.setPreferredSize(new Dimension(StyleConstants.WINDOW_WIDTH/5, 50*items.size()));
        menu.setLayout(new GridLayout(items.size(), 1));
        for(AbstractButton item : items) {
        	menu.add(item);
        }

        menu.addPopupMenuListener(new PopupMenuListener() {
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            	button.setSelected(false);
            }

            public void popupMenuCanceled(PopupMenuEvent e) {
            	button.setSelected(false);
            }
        });

        menu.show(component, 0, component.getHeight());
    }
	
	private void loadConfiguration() {
		try {
			properties = new Properties();
			FileInputStream in = new FileInputStream("properties/application.properties");
			properties.load(in);
			in.close();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
}
